package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Common;
import com.example.elderlycare.dao.entity.Evaluation;
import com.example.elderlycare.dao.mapper.CommonMapper;
import com.example.elderlycare.dao.mapper.EvaluationMapper;
import com.example.elderlycare.service.EvaluationService;
import com.example.elderlycare.service.entity.EvaluationItem;
import com.example.elderlycare.service.entity.EvaluationListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation>
        implements EvaluationService {
    @Resource
    EvaluationMapper evaluationMapper;

    @Resource
    CommonMapper commonMapper;

    @Override
    public EvaluationListResponseBody queryEvaluations(Map<String, Object> params) {
        List<Evaluation> evaluations = evaluationMapper.queryEvaluations(params);
        int total = evaluationMapper.getEvaluationsCount(params);
        List<EvaluationItem> evaluationItems = new ArrayList<>();

        QueryWrapper<Common> queryWrapper = new QueryWrapper<>();
        /* type = 13、14 分别对应评估类型字典、评估标准字典 */
        queryWrapper.eq("type", 13).or().eq("type", 14);
        List<Common> commonList = commonMapper.selectList(queryWrapper);
        Map<Integer, Common> commonMap = new HashMap<>();
        commonList.forEach((item) -> commonMap.put(item.getId(), item));

        evaluations.forEach((item) -> {
            EvaluationItem evaluationItem = EvaluationItem.builder()
                    .id(item.getId())
                    .elderId(item.getElderId())
                    .elderName(item.getElder().getName())
                    .certificateName(item.getElder().getCertificateName())
                    .elderNumber(item.getElder().getNumber())
                    .evaluator(item.getEvaluator())
                    .evaluateTypeId(item.getEvaluateTypeId())
                    .evaluateType(item.getEvaluateTypeId() != null ?
                            commonMap.get(item.getEvaluateTypeId()).getName() : null)
                    .evaluator(item.getEvaluator())
                    .date(item.getDate())
                    .place(item.getPlace())
                    .result(item.getResult())
                    .standardId(item.getStandardId())
                    .standard(item.getStandardId() != null ?
                            commonMap.get(item.getStandardId()).getName() : null)
                    .register(item.getRegister())
                    .build();
            evaluationItems.add(evaluationItem);

        });
        return EvaluationListResponseBody.builder()
                .items(evaluationItems)
                .total(total)
                .build();
    }
}
